﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using Portal.Web.Model;
using Portal.Web.Utility;
using Portal.Exam.Bussiness;
using Portal.Exam.Model;
using Portal.Core;

namespace Portal.Exam.Render
{
    public class UpdateQuestion
    {
        public static AjaxOut GetListAnswer(string SiteId, string QuestionId, string QuestionType)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                DataTable dtAnswers = new QuestionBll().ReadingAnswer(SiteId, QuestionId);

                string RetHtml = "<table>";
                for (int iIndexAns = 0; iIndexAns < dtAnswers.Rows.Count; iIndexAns++)
                {
                    string AnswerTextFirst = "???";
                    try
                    {
                        AnswerTextFirst = (string)dtAnswers.Rows[iIndexAns]["AnswerTextFirst"];
                    }
                    catch { }

                    string CorrectAnswer = "";
                    try
                    {
                        CorrectAnswer = (string)dtAnswers.Rows[iIndexAns]["CorrectAnswer"];
                    }
                    catch { }
                    CorrectAnswer = CorrectAnswer.ToUpper();
                    string Class = " class=cssIncorrectAnsLine";
                    string AnswerId = (string)dtAnswers.Rows[iIndexAns]["AnswerId"];

                    string SelectButton =
                        "<input style=\"width:120px\" onclick=javascript:SetRightAnsItem('" + QuestionId + "','" + AnswerId + "','" + QuestionType + "'); type=button class=button  value=\"=>chọn pán\">";
                    if (CorrectAnswer.Equals("YES"))
                    {
                        Class = " class=cssCorrectAnsLine";
                        SelectButton =
                                "<input style=\"width:120px\" onclick=javascript:RemoveRightAnsItem('" + QuestionId + "','" + AnswerId + "','" + QuestionType + "'); type=button class=button  value=\"=>bỏ chọn\">";
                    }
                    RetHtml +=
                            "<tr>" +
                            "      <td valign=top style=\"width:30px;\" align=right>pa." + (iIndexAns + 1) + ":</td>" +
                            "      <td valign=top style=\"width:80px;\">" + SelectButton + "</td>" +
                            "      <td valign=top style=\"width:30px;\"><input onclick=javascript:EditAnswerItemText('" + QuestionId + "','" + AnswerId + "','" + QuestionType + "'); type=button class=button  value=\"Sửa\"></td>" +
                            "      <td valign=top style=\"width:30px;\"><input onclick=javascript:ClientDeleteAns('" + QuestionId + "','" + AnswerId + "','" + QuestionType + "'); type=button class=button  value=\"Xóa\"></td>" +
                            "      <td valign=top><span" + Class + ">" + AnswerTextFirst + "</span></td>" +
                            "</tr>";

                }
                RetHtml += "</table>";
                OAjaxOut.HtmlContent = RetHtml;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.InfoMessage = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut GetListAnswerImage(string SiteId, string QuestionId, string QuestionType)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                DataTable dtAnswers = new QuestionBll().ReadingAnswer(SiteId, QuestionId);

                string RetHtml = "<table>";
                for (int iIndexAns = 0; iIndexAns < dtAnswers.Rows.Count; iIndexAns++)
                {
                    string AnswerTextFirst = "???";
                    string AnswerTextLast = string.Empty;
                    string Correct = string.Empty;

                    try
                    {
                        AnswerTextFirst = (string)dtAnswers.Rows[iIndexAns]["AnswerTextFirst"];
                    }
                    catch { }

                    try
                    {
                        AnswerTextLast = (string)dtAnswers.Rows[iIndexAns]["AnswerTextLast"];
                    }
                    catch { }

                    try
                    {
                        Correct = (string)dtAnswers.Rows[iIndexAns]["CorrectAnswer"];
                    }
                    catch { }

                    string AnswerId = (string)dtAnswers.Rows[iIndexAns]["AnswerId"];

                    Portal.Core.SiteParam OSiteParam = Portal.Web.Utility.WebEnvironments.CreateSiteParam(SiteId);
                    string HttpDataRoot = Portal.Web.Utility.WebConfig.HttpDataRoot(OSiteParam);
                    HttpDataRoot = HttpDataRoot + "/Cms/" + WebSessionUtility.GetCurrentLoginUser().AssetCode + "/" + QuestionId;
                    string ImageUrl = string.Empty;
                    if (!string.IsNullOrEmpty(AnswerTextLast))
                    {
                        ImageUrl = HttpDataRoot + "/" + Correct;
                    }
                    RetHtml +=
                         "<tr>" +
                            "      <td colspan=5>" +
                            "      <img src = '" + ImageUrl + "' class='imgexam' />" +
                            "       <input type=hidden id=\"txtUploadFileUpdate\" value='" + Correct + "'>\r\n" +
                            "       </td>" +
                            "</tr>" +
                            "<tr>" +
                            "      <td valign=top style=\"width:30px;\"><input onclick=javascript:EditAnswerItemText('" + QuestionId + "','" + AnswerId + "','" + QuestionType + "'); type=button class=button  value=\"Sửa\"></td>" +
                            "      <td valign=top style=\"width:30px;\"><input onclick=javascript:ClientDeleteAns('" + QuestionId + "','" + AnswerId + "','" + QuestionType + "'); type=button class=button  value=\"Xóa\"></td>" +

                            "      <td valign=top style='text-align:right'>" + AnswerTextFirst + "</td>" +
                            "      <td valign=top  style=\"width:30px;\"><========></td>" +

                            "      <td valign=top  style='text-align:left'>" +
                                    AnswerTextLast +
                            "       </td>" +

                            "</tr>";
                }
                RetHtml += "</table>";
                OAjaxOut.HtmlContent = RetHtml;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.InfoMessage = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        public static AjaxOut GetListAnswerCrossLink(string SiteId, string QuestionId, string QuestionType)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                DataTable dtAnswers = new QuestionBll().ReadingAnswer(SiteId, QuestionId);

                string RetHtml = "<table>";
                for (int iIndexAns = 0; iIndexAns < dtAnswers.Rows.Count; iIndexAns++)
                {
                    string AnswerTextFirst = "???";
                    string AnswerTextLast = string.Empty;

                    try
                    {
                        AnswerTextFirst = (string)dtAnswers.Rows[iIndexAns]["AnswerTextFirst"];
                    }
                    catch { }

                    try
                    {
                        AnswerTextLast = (string)dtAnswers.Rows[iIndexAns]["AnswerTextLast"];
                    }
                    catch { }

                    string CorrectAnswer = "";
                    try
                    {
                        CorrectAnswer = (string)dtAnswers.Rows[iIndexAns]["CorrectAnswer"];
                    }
                    catch { }
                    CorrectAnswer = CorrectAnswer.ToUpper();
                    string Class = " class=cssIncorrectAnsLine";
                    string AnswerId = (string)dtAnswers.Rows[iIndexAns]["AnswerId"];

                    if (CorrectAnswer.Equals("YES"))
                    {
                        Class = " class=cssCorrectAnsLine";
                    }
                    RetHtml +=
                            "<tr>" +
                            "      <td valign=top style=\"width:30px;\"><input onclick=javascript:EditAnswerItemText('" + QuestionId + "','" + AnswerId + "','" + QuestionType + "'); type=button class=button  value=\"Sửa\"></td>" +
                            "      <td valign=top style=\"width:30px;\"><input onclick=javascript:ClientDeleteAns('" + QuestionId + "','" + AnswerId + "','" + QuestionType + "'); type=button class=button  value=\"Xóa\"></td>" +

                            "      <td valign=top style='text-align:right'>" + AnswerTextFirst + "</td>" +

                            "      <td valign=top  style=\"width:30px;\"><========></td>" +

                            "      <td valign=top style='text-align:left'>" + AnswerTextLast + "</td>" +

                            "</tr>";

                }
                RetHtml += "</table>";
                OAjaxOut.HtmlContent = RetHtml;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.InfoMessage = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                string QuestionId = System.Web.HttpContext.Current.Request["QuestionId"];
                string GroupQuestionId = System.Web.HttpContext.Current.Request["GroupQuestionId"];
                string RootGroupQuestionId = System.Web.HttpContext.Current.Request["RootGroupQuestionId"];

                if (string.IsNullOrEmpty(QuestionId)) throw new Exception("Truy cập bị cấm đường dẫn không hợp lệ");
                QuestionCls QuestionCls = new QuestionBll().CreateModel(SiteId, QuestionId);
                if (QuestionCls == null) throw new Exception("Truy cập bị cấm đường dẫn không hợp lệ");

                string[] Answer = new string[]{
                    "Đúng","Sai"
                };
                string[] AnswerCode = new string[]{
                    "Yes","No"
                };

                //string Type = (string)WebEnvironments.Request(SiteId, "Type");

                string SelectLevel =
                      "<select id=drpSelectLevelQuestion class=drplist>\r\n" +
                      "   <option value=\"0\">Mức độ dễ</option>\r\n" +
                      "   <option value=\"1\">Mức độ trung bình</option>\r\n" +
                      "   <option value=\"2\">Mức độ khó</option>\r\n" +
                      "</select>\r\n";
                if (QuestionCls.Level == 1)
                {
                    SelectLevel =
                      "<select id=drpSelectLevelQuestion class=drplist>\r\n" +
                      "   <option value=\"0\">Mức độ dễ</option>\r\n" +
                      "   <option value=\"1\" selected>Mức độ trung bình</option>\r\n" +
                      "   <option value=\"2\">Mức độ khó</option>\r\n" +
                      "</select>\r\n";
                }
                if (QuestionCls.Level == 2)
                {
                    SelectLevel =
                      "<select id=drpSelectLevelQuestion class=drplist>\r\n" +
                      "   <option value=\"0\">Mức độ dễ</option>\r\n" +
                      "   <option value=\"1\">Mức độ trung bình</option>\r\n" +
                      "   <option value=\"2\" selected>Mức độ khó</option>\r\n" +
                      "</select>\r\n";
                }

                string RightAns = "";
                string TrueAns = "Các phương án đúng: ";
                string FalseAns = "Các phương án sai: ";

                string CrossAns = "Cặp đáp án: ";

                DataTable dtAnswers = new QuestionBll().ReadingAnswer(SiteId, QuestionId);
                for (int iIndexAns = 0; iIndexAns < dtAnswers.Rows.Count; iIndexAns++)
                {
                    string CorrectAnswer = "";
                    try
                    {
                        CorrectAnswer = (string)dtAnswers.Rows[iIndexAns]["CorrectAnswer"];
                    }
                    catch { }
                    CorrectAnswer = CorrectAnswer.ToUpper();
                    if (CorrectAnswer.Equals("YES"))
                    {
                        switch (QuestionCls.Type.ToLower())
                        {
                            case "multichoice":
                                if (!string.IsNullOrEmpty(RightAns)) RightAns += "; ";
                                RightAns = RightAns + (string)dtAnswers.Rows[iIndexAns]["AnswerTextFirst"];
                                break;

                            case "bestanswer":
                                if (!string.IsNullOrEmpty(RightAns)) RightAns += "; ";
                                RightAns = RightAns + (string)dtAnswers.Rows[iIndexAns]["AnswerTextFirst"];
                                break;

                            case "truefalse":
                                TrueAns += (string)dtAnswers.Rows[iIndexAns]["AnswerTextFirst"] + ";";
                                break;

                            case "crosslink":
                                CrossAns += (string)dtAnswers.Rows[iIndexAns]["AnswerTextFirst"] +
                                            " - " +
                                            (string)dtAnswers.Rows[iIndexAns]["AnswerTextLast"] + ";";
                                break;

                            case "image":
                                CrossAns += (string)dtAnswers.Rows[iIndexAns]["AnswerTextFirst"] +
                                            " - " +
                                            (string)dtAnswers.Rows[iIndexAns]["AnswerTextLast"] + ";";
                                break;
                        }
                    }
                    else
                    {
                        if (QuestionCls.Type.ToLower().Equals("TrueFalse".ToLower()))
                        {
                            FalseAns += (string)dtAnswers.Rows[iIndexAns]["AnswerTextFirst"] + ";";
                        }

                    }
                }
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string IconRoot = WebConfig.GetButtonIconRoot(OSiteParam);

                string UploadHandler = "CmsUploadHandler.ashx";

                //string previewUrl = "PreviewOneQuestion.aspx?QuestionId="+QuestionId;
                string BackUrl = "GroupQuestionTreeView.aspx?RId=" + RootGroupQuestionId;// "ExamQuestion.aspx?GroupQuestionId=" + QuestionCls.MonthiCls.GroupQuestionCls.GroupQuestionId + "&MonthiId=" + QuestionCls.MonthiCls.MonthiId + "&Type=" + QuestionCls.Type;
                string RetHtml =

                #region javascript
 " <script language=javascript> \r\n" +

                     "function fileSelected() {\r\n" +
                    "    var file = document.getElementById('fileToUpload').files[0];\r\n" +
                    "    if (file) {\r\n" +
                    "        var fileSize = 0;\r\n" +
                    "        if (file.size > 1024 * 1024)\r\n" +
                    "            fileSize = (Math.round(file.size * 100 / (1024 * 1024)) / 100).toString() + 'MB';\r\n" +
                    "        else\r\n" +
                    "            fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB';\r\n" +
                    "        document.getElementById('fileName').innerHTML = 'Tên tệp: ' + file.name;\r\n" +
                    "        document.getElementById('fileSize').innerHTML = 'Kích cỡ: ' + fileSize;\r\n" +
                    "        document.getElementById('fileType').innerHTML = 'Loại tệp: ' + file.type;\r\n" +
                    "    }\r\n" +
                    "}\r\n" +



                    "function uploadFile() {\r\n" +
                    //"    if(confirm('Bạn đã chắc chắn nhập dữ liệu từ file chưa?')==false)return;\r\n" +
                    "    var fd = new FormData();\r\n" +
                    "    fd.append(\"fileToUpload\", document.getElementById('fileToUpload').files[0]);\r\n" +
                    "    var xhr = new XMLHttpRequest();\r\n" +
                    "    xhr.upload.addEventListener(\"progress\", uploadProgress, false);\r\n" +
                    "    xhr.addEventListener(\"load\", uploadComplete, false);\r\n" +
                    "    xhr.addEventListener(\"error\", uploadFailed, false);\r\n" +
                    "    xhr.addEventListener(\"abort\", uploadCanceled, false);\r\n" +
                    //"    xhr.open(\"POST\", \"UploadHandler.ashx?SiteId=" + SiteId + "\");\r\n" +
                    "    xhr.open(\"POST\", \"" + UploadHandler + "?sid=" + SiteId + "&uni=" + WebSessionUtility.GetCurrentLoginUser().AssetCode + "&gid=" + QuestionId + "&dataid=" + QuestionCls.GroupQuestionId + "\");\r\n" +
                    "    xhr.send(fd);\r\n" +
                    "}\r\n" +

                    "function uploadProgress(evt) {\r\n" +
                    "    if (evt.lengthComputable) {\r\n" +
                    "    }\r\n" +
                    "    else {\r\n" +
                    "    }\r\n" +
                    "}\r\n" +

                    "function uploadComplete(evt) {\r\n" +
                    "    if(evt.target.responseText=='')\r\n" +
                    "    {\r\n" +
                    "       alert('Chưa chọn file để import!');\r\n" +
                    "       return;\r\n" +
                    "    }\r\n" +
                    "    /* This event is raised when the server send back a response */\r\n" +
                    "    document.getElementById('txtUploadFile').value=evt.target.responseText;\r\n" +
                    "    //doan nay chay upload vao day...\r\n" +
                    "       alert('Upload ảnh thành công, hãy nhập các câu hỏi và trả lời tương ứng với hình ảnh!');\r\n" +
                    "    UpdateItemAnswerImage();\r\n" +

                    "}\r\n" +

                    "function uploadFailed(evt) {\r\n" +
                    "    alert(\"Lỗi khi tải tệp dữ liệu lên máy chủ.\");\r\n" +
                    "}\r\n" +

                    "function uploadCanceled(evt) {\r\n" +
                    "    alert(\"Người dùng hủy bỏ tải lên máy chủ hoặc mất kết nối với máy chủ.\");\r\n" +
                    "}\r\n" +


                    " function AddItemAnswer(QuestionId,QuestionType) \r\n" +
                    "     { \r\n" +
                    "         SiteId='" + SiteId + "';\r\n" +
                    "         NewAnswerItem=document.getElementById(\"txtNewAnswerItem\").value; \r\n" +
                    "         AjaxOut = Portal.Exam.Render.UpdateQuestion.ServerSideAddNewAnswerItem(SiteId, QuestionId,NewAnswerItem,QuestionType).value; \r\n" +
                    "         if(AjaxOut.Error) \r\n" +
                    "         { \r\n" +
                    "             alert(AjaxOut.InfoMessage); \r\n" +
                    "             return; \r\n" +
                    "         } \r\n" +
                    "         document.getElementById(\"divListAnswer\").innerHTML=AjaxOut.HtmlContent; \r\n" +
                    "         document.getElementById(\"txtNewAnswerItem\").value=''; \r\n" +
                    "         document.getElementById(\"txtNewAnswerItem\").focus(); \r\n" +
                    "     } \r\n" +

                    " function AddItemAnswerCrossLink(QuestionId, QuestionType) \r\n" +
                    "     { \r\n" +
                    "         SiteId='" + SiteId + "';\r\n" +
                    "         NewAnswerItemPA1=document.getElementById(\"txtNewAnswerItemPA1\").value; \r\n" +
                    "         NewAnswerItemPA2=document.getElementById(\"txtNewAnswerItemPA2\").value; \r\n" +
                    "         AjaxOut = Portal.Exam.Render.UpdateQuestion.ServerSideAddNewAnswerCrossLinkItem(SiteId, QuestionId,NewAnswerItemPA1,NewAnswerItemPA2,QuestionType).value; \r\n" +
                    "         if(AjaxOut.Error) \r\n" +
                    "         { \r\n" +
                    "             alert(AjaxOut.InfoMessage); \r\n" +
                    "             return; \r\n" +
                    "         } \r\n" +
                    "         document.getElementById(\"divListAnswer\").innerHTML=AjaxOut.HtmlContent; \r\n" +
                    "         document.getElementById(\"txtNewAnswerItemPA1\").value=''; \r\n" +
                    "         document.getElementById(\"txtNewAnswerItemPA2\").value=''; \r\n" +
                    "         document.getElementById('fileName').value = '';\r\n" +
                    "         document.getElementById('fileSize').value = '';\r\n" +
                    "         document.getElementById('fileType').value= '';\r\n" +

                    "         document.getElementById(\"txtNewAnswerItemPA1\").focus(); \r\n" +
                    "     } \r\n" +

                     " function AddItemAnswerImage(QuestionId, QuestionType) \r\n" +
                    "     { \r\n" +
                    "         SiteId='" + SiteId + "';\r\n" +
                    "         NewAnswerItemPA1=document.getElementById(\"txtNewAnswerItemPA1\").value; \r\n" +
                    "         NewAnswerItemPA2=document.getElementById(\"txtNewAnswerItemPA2\").value; \r\n" +
                    "         if(document.getElementById('txtUploadFile')!=null)\r\n" +
                    "         {\r\n" +
                    "               UploadFile=document.getElementById('txtUploadFile').value;\r\n" +
                    "         }\r\n" +

                    "         if(!UploadFile)\r\n" +
                    "         {\r\n" +
                    "           if(document.getElementById('txtUploadFileUpdate')!=null)\r\n" +
                    "           {\r\n" +
                    "               UploadFile=document.getElementById('txtUploadFileUpdate').value;\r\n" +
                    "           }\r\n" +
                    "         }\r\n" +

                    "         AjaxOut = Portal.Exam.Render.UpdateQuestion.ServerSideAddNewAnswerImage(SiteId, QuestionId,NewAnswerItemPA1,NewAnswerItemPA2,QuestionType,UploadFile).value; \r\n" +
                    "         if(AjaxOut.Error) \r\n" +
                    "         { \r\n" +
                    "             alert(AjaxOut.InfoMessage); \r\n" +
                    "             return; \r\n" +
                    "         } \r\n" +
                    "         document.getElementById(\"divListAnswer\").innerHTML=AjaxOut.HtmlContent; \r\n" +
                    "         document.getElementById(\"txtNewAnswerItemPA1\").value=''; \r\n" +
                    "         document.getElementById(\"txtNewAnswerItemPA2\").value=''; \r\n" +
                    "         document.getElementById('fileName').value = '';\r\n" +
                    "         document.getElementById('fileSize').value = '';\r\n" +
                    "         document.getElementById('fileType').value= '';\r\n" +

                    "         document.getElementById(\"txtNewAnswerItemPA1\").focus(); \r\n" +
                    "     } \r\n" +


                    " function UpdateItemAnswerImage() \r\n" +
                    "     { \r\n" +
                    "         SiteId='" + SiteId + "';\r\n" +
                    "         QuestionId='" + QuestionId + "';\r\n" +
                    "         QuestionType='" + QuestionCls.Type + "';\r\n" +
                    "         UploadFile='';\r\n" +
                    "         UploadFile=document.getElementById('txtUploadFileUpdate').value;\r\n" +

                    "         if(document.getElementById('txtUploadFile')!=null)\r\n" +
                    "         {\r\n" +
                    "               UploadFile=document.getElementById('txtUploadFile').value;\r\n" +
                    "         }\r\n" +
                    "         AjaxOut = Portal.Exam.Render.UpdateQuestion.ServerSideUpdateAnswerImage(SiteId, QuestionId,UploadFile).value; \r\n" +
                    "         if(AjaxOut.Error) \r\n" +
                    "         { \r\n" +
                    "             alert(AjaxOut.InfoMessage); \r\n" +
                    "             return; \r\n" +
                    "         } \r\n" +
                    "         document.getElementById(\"divListAnswer\").innerHTML=AjaxOut.HtmlContent; \r\n" +
                    "         document.getElementById(\"txtNewAnswerItemImage\").value=''; \r\n" +
                    "         document.getElementById(\"fileToUpload\").value=''; \r\n" +
                    "         document.getElementById(\"txtUploadFile\").focus(); \r\n" +
                    "     } \r\n" +


                    //" function AddItemAnswerImage() \r\n" +
                    //"     { \r\n" +
                    //"         SiteId='" + SiteId + "';\r\n" +
                    //"         QuestionId='" + QuestionId + "';\r\n" +
                    //"         QuestionType='" + QuestionCls.Type + "';\r\n" +
                    //"         NewAnswerItemImage=document.getElementById(\"txtNewAnswerItemImage\").value; \r\n" +
                    //"         UploadFile='';\r\n" +
                    //"         if(document.getElementById('txtUploadFile')!=null)\r\n" +
                    //"         {\r\n" +
                    //"               UploadFile=document.getElementById('txtUploadFile').value;\r\n" +
                    //"         }\r\n" +
                    //"         AjaxOut = Portal.Exam.Render.UpdateQuestion.ServerSideAddNewAnswerImage(SiteId, QuestionId,NewAnswerItemImage,UploadFile,QuestionType).value; \r\n" +
                    //"         if(AjaxOut.Error) \r\n" +
                    //"         { \r\n" +
                    //"             alert(AjaxOut.InfoMessage); \r\n" +
                    //"             return; \r\n" +
                    //"         } \r\n" +
                    //"         document.getElementById(\"divListAnswer\").innerHTML=AjaxOut.HtmlContent; \r\n" +
                    //"         document.getElementById(\"txtNewAnswerItemImage\").value=''; \r\n" +
                    //"         document.getElementById(\"fileToUpload\").value=''; \r\n" +
                    //"         document.getElementById(\"txtUploadFile\").focus(); \r\n" +
                    //"     } \r\n" +


                    " function ClientDeleteAns(QuestionId,AnswerId,QuestionType) \r\n" +
                    " { \r\n" +
                    "     SiteId='" + SiteId + "';\r\n" +
                    "     if(confirm('Bạn đã chắc chắn xóa chưa ?')==false)return; \r\n" +
                    "     AjaxOut = Portal.Exam.Render.UpdateQuestion.ServerSideDeleteAnswer(SiteId, AnswerId, QuestionId,QuestionType).value; \r\n" +
                    "     if(AjaxOut.Error)\r\n" +
                    "     {\r\n" +
                    "         alert(AjaxOut.InfoMessage);\r\n" +
                    "         return;\r\n" +
                    "     }\r\n" +
                    "     document.getElementById(\"divListAnswer\").innerHTML=AjaxOut.HtmlContent;\r\n" +
                    " } \r\n" +


                    " function EditAnswerItemText(QuestionId,AnswerId,QuestionType)\r\n" +
                    " {\r\n" +
                    "     SiteId='" + SiteId + "';\r\n" +
                    "     Url=Portal.Exam.Render.UpdateQuestion.GetUpdateAnsItemText(SiteId, QuestionId,AnswerId,QuestionType).value;\r\n" +
                    "     if(Url.indexOf(\"ERROR\")!=-1)\r\n" +
                    "     {\r\n" +
                    "         alert(Url);\r\n" +
                    "         return;\r\n" +
                    "     }\r\n" +
                    "     window.open(Url, '_self'); \r\n" +
                    " }\r\n" +

                    " function ClientSaveUpdateQuestionQuestion(QuestionId)\r\n" +
                    " {\r\n" +
                    "     SiteId='" + SiteId + "';\r\n" +
                    "     Title=document.getElementById(\"txtTitle\").value;\r\n" +
                    "     oEditor = FCKeditorAPI.GetInstance('FCKeditorBody');\r\n" +
                    "     bodyText= oEditor.GetXHTML( true ); \r\n" +

                    "     MaxPoint=document.getElementById(\"txtPoint\").value;\r\n" +
                    "     SelectLevel = document.getElementById(\"drpSelectLevelQuestion\").value; \r\n" +
                    "     retValue=Portal.Exam.Render.UpdateQuestion.ServerSideSaveQuestion(SiteId, QuestionId,Title, bodyText,MaxPoint, SelectLevel).value;\r\n" +
                    "     if(retValue.indexOf(\"ERROR\")!=-1)\r\n" +
                    "     {\r\n" +
                    "         alert(retValue);\r\n" +
                    "         return;\r\n" +
                    "     }\r\n" +
                    "     alert(retValue);\r\n" +
                    " }\r\n" +

                    " function SetRightAnsItem(QuestionId,AnswerId, QuestionType)\r\n" +
                    " {\r\n" +
                    "     SiteId='" + SiteId + "';\r\n" +
                    "     AjaxOut=Portal.Exam.Render.UpdateQuestion.ServerSideSetRightAnsItem(SiteId, QuestionId,AnswerId,QuestionType).value;\r\n" +
                    "     if(AjaxOut.Error)\r\n" +
                    "     {\r\n" +
                    "         alert(AjaxOut.InfoMessage);\r\n" +
                    "         return;\r\n" +
                    "     }\r\n" +
                    "     document.getElementById(\"divListAnswer\").innerHTML=AjaxOut.HtmlContent;\r\n" +
                    "     document.getElementById(\"divRightAns\").innerHTML=AjaxOut.RetExtraParam1;\r\n" +
                    " }\r\n" +

                    " function RemoveRightAnsItem(QuestionId,AnswerId,QuestionType)\r\n" +
                    " {\r\n" +
                    "     SiteId='" + SiteId + "';\r\n" +
                    "     AjaxOut=Portal.Exam.Render.UpdateQuestion.ServerSideRemoveRightAnsItem(SiteId, QuestionId,AnswerId,QuestionType).value;\r\n" +
                    "     if(AjaxOut.Error)\r\n" +
                    "     {\r\n" +
                    "         alert(AjaxOut.InfoMessage);\r\n" +
                    "         return;\r\n" +
                    "     }\r\n" +
                    "     document.getElementById(\"divListAnswer\").innerHTML=AjaxOut.HtmlContent;\r\n" +
                    "     document.getElementById(\"divRightAns\").innerHTML=AjaxOut.RetExtraParam1;\r\n" +
                    " }\r\n" +


                    " function ClientAddNewQuestionQuestion(MonthiId,QuestionId)\r\n" +
                    " {\r\n" +
                    "       window.open('AddQuestion.aspx?MonthiId='+MonthiId,'_self');\r\n" +
                    " }\r\n" +
                    " </script>\r\n" +

                #endregion

 " <div class=DatabaseFunction>\r\n" +
                    "        <ul>\r\n" +
                    "            <li onclick=\"javascript:ClientSaveUpdateQuestionQuestion('" + QuestionId + "');\"><img class=image src=\"" + IconRoot + "/Save.png\"><div>Ghi lại</div></li>\r\n" +
                    //"            <li onclick=\"javascript:ClientAddNewQuestionQuestion('" + QuestionId + "');\"><img class=image src=\""+IconRoot+"/Add.png\"><div>Câu hỏi mới</div></li>\r\n" +
                    "            <li onclick=\"javascript:window.open('" + BackUrl + "','_self');\"><img class=image src=\"" + IconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                    "        </ul>\r\n" +
                    "  </div>\r\n" +
                    "   <h3>Cập nhật ngân hàng câu hỏi</h3>\r\n" +
                    "   <div class=CssFormSaveUpdate>\r\n" +
                    "      <table>" +
                    "         <tr>" +
                    "              <td class=Col1>Mức độ</td>" +
                    "              <td class=Col2>" + SelectLevel + "</td>" +
                    "         </tr>" +
                    "         <tr>" +
                    "              <td class=Col1>Tiêu đề câu hỏi</td>" +
                    "              <td class=Col2><input id=txtTitle type=textbox class=textbox value=\"" + QuestionCls.Title + "\"></td>" +
                    "         </tr>" +
                    "          <tr>" +
                    "              <td class=Col1>Câu hỏi:</td>" +
                    "              <td class=Col2>\r\n" +
                    "               <textarea type=hidden id=\"FCKeditorBody\" name=\"FCKeditorBody\">" + QuestionCls.Question + "</textarea>\r\n" +
                    "               <script type=\"text/javascript\" src=\"/fckeditor/fckeditor.js\"></script>\r\n" +
                    "               <script type=\"text/javascript\">\r\n" +
                    "                   var oFCKeditor = new FCKeditor('FCKeditorBody');\r\n" +
                    "                   oFCKeditor.BasePath = \"/fckeditor/\";\r\n" +
                    "                   oFCKeditor.Height = \"500\";\r\n" +
                    "                   oFCKeditor.ReplaceTextarea();\r\n" +
                    "               </script>\r\n" +
                    "              </td>" +
                    "          </tr>" +
                    "          <tr>" +
                    "              <td class=Col1>Điểm:</td>" +
                    "              <td class=Col2><input class=textbox id=txtPoint value=\"" + QuestionCls.MaxPoint + "\"></td>" +
                    "          </tr>";
                //"          <tr>" +
                //"              <td>Thời gian(giây):</td>" +
                //"              <td><input class=textbox id=txtSecondTime value=\"" + QuestionCls.SecondTime + "\"></td>" +
                //"          </tr>" +
                if (!QuestionCls.Type.ToLower().Equals("FreeText".ToLower()))
                {
                    if (QuestionCls.Type.ToLower().Equals("TrueFalse".ToLower()))
                    {
                        RightAns = TrueAns + " - " + FalseAns;
                    }
                    RetHtml +=
                        "          <tr>" +
                        "              <td>Đáp án:</td>" +
                        "              <td><div id=\"divRightAns\">" + RightAns + "</div></td>" +
                        "          </tr>";

                }
                RetHtml +=
                    //"          <tr>" +
                    //"              <td>Câu hỏi trí nhớ:</td>" +
                    //"              <td><input class=checkbox type=checkbox id=chkIsMemory " + (QuestionCls.IsMemory == 1 ? "CHECKED" : "") + " ></td>" +
                    //"          </tr>" +
                    //"          <tr>" +
                    //"              <td>Thời gian ẩn:</td>" +
                    //"              <td><input class=textbox id=txtSecondHidden value=\"" + QuestionCls.SecondHidden + "\"></td>" +
                    //"          </tr>" +
                    "         <tr>" +
                    "             <td></td>" +
                    "             <td></td>" +
                    "         </tr>" +
                    "</table>" +
                    "</div>\r\n";
                if (QuestionCls.Type.ToLower().Equals("BestAnswer".ToLower()) ||
                    QuestionCls.Type.ToLower().Equals("TrueFalse".ToLower()) ||
                    QuestionCls.Type.ToLower().Equals("Multichoice".ToLower()))
                {
                    RetHtml +=
                        "<div class=CssListAnsResult>\r\n" +
                        "   <h3>CÁC PHƯƠNG ÁN TRẢ LỜI</h3>" +
                        "   <div class=cssHeadingAddItemBestAns><input class=cssNewAnswerItem id=txtNewAnswerItem><input onclick=javascript:AddItemAnswer('" + QuestionId + "','" + QuestionCls.Type + "'); type=button class=button  value=\"Thêm phương án\"></div>" +
                        "   <div id=divListAnswer class=\"tabcontent\">\r\n" +
                                GetListAnswer(SiteId, QuestionId, QuestionCls.Type).HtmlContent +
                        "   </div>\r\n" +
                        "</div>\r\n";
                }
                // Crosslink question type
                else if (QuestionCls.Type.ToLower().Equals("crosslink"))
                {
                    RetHtml +=
                    "<div class=CssListAnsResult>\r\n" +
                    "   <h3>CÁC PHƯƠNG ÁN TRẢ LỜI</h3>" +
                    "   <div class=cssHeadingAddItemBestAns> " +
                    "       <input class=cssNewAnswerItemCross id=txtNewAnswerItemPA1> " +
                    "       <input class=cssNewAnswerItemCross id=txtNewAnswerItemPA2> " +
                    "   <input onclick=javascript:AddItemAnswerCrossLink('" + QuestionId + "','" + QuestionCls.Type + "'); type=button class=button  value=\"Thêm phương án\">" +
                    "   </div>" +

                    "       <div id=divListAnswer class=\"tabcontent\">\r\n" +
                                GetListAnswerCrossLink(SiteId, QuestionId, QuestionCls.Type).HtmlContent +
                    "       </div>\r\n" +

                    "</div>\r\n";
                }
                else if (QuestionCls.Type.ToLower().Equals("image"))
                {
                    RetHtml +=
                    "<div class=CssListAnsResult>\r\n" +
                    "   <h3>Chọn file ảnh</h3>" +
                    "   <div class=c2> " +
                    "       <input type=hidden id=\"txtUploadFile\"><input  class='cssNewAnswerItemCross' type=\"file\" name=\"fileToUpload[]\" id=\"fileToUpload\"  onchange=\"fileSelected();\" /> \r\n" +
                    "       <div id=\"fileName\">\r\n" +
                    "       </div>\r\n" +
                    "       <div id=\"fileSize\">\r\n" +
                    "       </div>\r\n" +
                    "       <div id=\"fileType\">\r\n" +
                    "       </div>\r\n" +
                    "       <div id=divProcessing></div>\r\n" +
                    "   <input onclick=javascript:uploadFile(); type=button class=button  value=\"Đồng ý\">" +
                    "   <div/> " +

                    "   <h3>CÁC PHƯƠNG ÁN TRẢ LỜI</h3>" +
                    "   <div class=cssHeadingAddItemBestAns> " +
                    "       <input class=cssNewAnswerItemCross id=txtNewAnswerItemPA1> " +
                    "       <input class=cssNewAnswerItemCross id=txtNewAnswerItemPA2> " +
                    "   <input onclick=javascript:AddItemAnswerImage('" + QuestionId + "','" + QuestionCls.Type + "'); type=button class=button  value=\"Thêm phương án\">" +

                  "   </div>" +
                    "       <div id=divListAnswer class=\"tabcontent\">\r\n" +
                                GetListAnswerImage(SiteId, QuestionId, QuestionCls.Type).HtmlContent +
                    "       </div>\r\n" +
                    "</div>\r\n";

                    // Image question type
                    //RetHtml +=
                    //"<div class=CssListAnsResult>\r\n" +
                    //"   <h3>CÁC PHƯƠNG ÁN TRẢ LỜI</h3>" +
                    //"   <div class=cssHeadingAddItemBestAns> " +
                    //"       <input class=cssNewAnswerItemCross id=txtNewAnswerItemPA1> " +
                    //"       <input class=cssNewAnswerItemCross id=txtNewAnswerItemPA2> " +

                    //"   <div class=c1> " +
                    //"       <input class=cssNewAnswerItemCross id=txtNewAnswerItemImage> " +
                    //"   </div> " +
                    //"   <div class=c2> " +
                    //"       <input type=hidden id=\"txtUploadFile\"><input  class='cssNewAnswerItemCross' type=\"file\" name=\"fileToUpload[]\" id=\"fileToUpload\"  onchange=\"fileSelected();\" /> \r\n" +
                    //"       <div id=\"fileName\">\r\n" +
                    //"       </div>\r\n" +
                    //"       <div id=\"fileSize\">\r\n" +
                    //"       </div>\r\n" +
                    //"       <div id=\"fileType\">\r\n" +
                    //"       </div>\r\n" +
                    //"       <div id=divProcessing></div>\r\n" +
                    //"   <div/> " +

                    //"   <input onclick=javascript:AddItemAnswerImage('" + QuestionId + "','" + QuestionCls.Type + "'); type=button class=button  value=\"Thêm phương án\">" +
                    //"   <input onclick=javascript:uploadFile(); type=button class=button  value=\"Thêm phương án\">" +
                    //RetHtml +=
                    //"   </div>" +
                    //"       <div id=divListAnswer class=\"tabcontent\">\r\n" +
                    //            GetListAnswerImage(SiteId, QuestionId, QuestionCls.Type).HtmlContent +
                    //"       </div>\r\n" +
                    //"</div>\r\n";
                }

                //else if (QuestionCls.Type.ToLower().Equals("image"))
                //{
                //    // Image question type
                //    RetHtml +=
                //    "<div class=CssListAnsResult>\r\n" +
                //    "   <h3>CÁC PHƯƠNG ÁN TRẢ LỜI</h3>" +
                //    "   <div class=cssHeadingAddItemBestAns> " +
                //    "   <div class=c1> " +
                //    "       <input class=cssNewAnswerItemCross id=txtNewAnswerItemImage> " +
                //    "   </div> " +
                //    "   <div class=c2> " +
                //    "       <input type=hidden id=\"txtUploadFile\"><input  class='cssNewAnswerItemCross' type=\"file\" name=\"fileToUpload[]\" id=\"fileToUpload\"  onchange=\"fileSelected();\" /> \r\n" +
                //    "       <div id=\"fileName\">\r\n" +
                //    "       </div>\r\n" +
                //    "       <div id=\"fileSize\">\r\n" +
                //    "       </div>\r\n" +
                //    "       <div id=\"fileType\">\r\n" +
                //    "       </div>\r\n" +
                //    "       <div id=divProcessing></div>\r\n" +
                //    "   <div/> " +

                //    //"   <input onclick=javascript:AddItemAnswerImage('" + QuestionId + "','" + QuestionCls.Type + "'); type=button class=button  value=\"Thêm phương án\">" +
                //    "   <input onclick=javascript:uploadFile(); type=button class=button  value=\"Thêm phương án\">" +
                //    "   </div>" +
                //    "       <div id=divListAnswer class=\"tabcontent\">\r\n" +
                //                GetListAnswerImage(SiteId, QuestionId, QuestionCls.Type).HtmlContent +
                //    "       </div>\r\n" +
                //    "</div>\r\n";
                //}


                OAjaxOut.HtmlContent = RetHtml;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod]
        public AjaxOut ServerSideAddNewAnswerItem(
            string SiteId,
            string QuestionId,
            string NewAnswerItem,
            string QuestionType)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                if (NewAnswerItem.Trim().Length == 0)
                {
                    throw new Exception("Chưa nhập phương án trả lời mới");
                }
                NewAnswerItem = NewAnswerItem.Trim();
                new QuestionBll().AddNewAnswerItem(null, SiteId, QuestionId, NewAnswerItem);

                OAjaxOut.HtmlContent = GetListAnswer(SiteId, QuestionId, QuestionType).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod]
        public AjaxOut ServerSideAddNewAnswerCrossLinkItem(
            string SiteId,
            string QuestionId,
            string NewAnswerItemPA1,
            string NewAnswerItemPA2,
            string QuestionType)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                if (NewAnswerItemPA1.Trim().Length == 0)
                {
                    throw new Exception("Chưa nhập phương án trả lời mới");
                }

                if (NewAnswerItemPA2.Trim().Length == 0)
                {
                    throw new Exception("Chưa nhập phương án trả lời mới");
                }

                new QuestionBll().AddNewAnswerCrossLinkItem(null, SiteId, QuestionId, NewAnswerItemPA1.Trim(), NewAnswerItemPA2.Trim());

                OAjaxOut.HtmlContent = GetListAnswerCrossLink(SiteId, QuestionId, QuestionType).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }
        
            [AjaxPro.AjaxMethod]
        public AjaxOut ServerSideUpdateAnswerImage(
            string SiteId,
            string QuestionId,                       
            string ImgFileName)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                
                if (ImgFileName.Trim().Length == 0)
                {
                    throw new Exception("Chưa chon file");
                }

                new QuestionBll().UpdateAnswerImageItem(null, SiteId, QuestionId, ImgFileName.Trim());

                OAjaxOut.HtmlContent = GetListAnswerImage(SiteId, QuestionId, "image").HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod]
        public AjaxOut ServerSideAddNewAnswerImage(
            string SiteId,
            string QuestionId,
            string NewAnswerItemPA1,
            string NewAnswerItemPA2,
            string QuestionType,
            string ImgFileName)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                if (NewAnswerItemPA1.Trim().Length == 0 || NewAnswerItemPA2.Trim().Length == 0)
                {
                    throw new Exception("Chưa nhập phương án trả lời mới");
                }

                if (ImgFileName.Trim().Length == 0)
                {
                    throw new Exception("Chưa chon file");
                }

                new QuestionBll().AddNewAnswerImageItem(null, SiteId, QuestionId, NewAnswerItemPA1.Trim(), NewAnswerItemPA2.Trim(), ImgFileName.Trim());

                OAjaxOut.HtmlContent = GetListAnswerImage(SiteId, QuestionId, QuestionType).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        //[AjaxPro.AjaxMethod]
        //public AjaxOut ServerSideAddNewAnswerImage(
        //    string SiteId,
        //    string QuestionId,
        //    string NewAnswerItemPA1,
        //    string NewAnswerItemPA2,
        //    string QuestionType)
        //{
        //    AjaxOut OAjaxOut = new AjaxOut();
        //    try
        //    {
        //        WebSessionUtility.CheckSessionTimeOut();
        //        if (NewAnswerItemPA1.Trim().Length == 0)
        //        {
        //            throw new Exception("Chưa nhập phương án trả lời mới");
        //        }

        //        if (NewAnswerItemPA2.Trim().Length == 0)
        //        {
        //            throw new Exception("Chưa chon file");
        //        }

        //        new QuestionBll().AddNewAnswerCrossLinkItem(null, SiteId, QuestionId, NewAnswerItemPA1.Trim(), NewAnswerItemPA2.Trim());

        //        OAjaxOut.HtmlContent = GetListAnswerImage(SiteId, QuestionId, QuestionType).HtmlContent;
        //    }
        //    catch (Exception ex)
        //    {
        //        OAjaxOut.Error = true;
        //        OAjaxOut.InfoMessage = ex.Message.ToString();
        //        OAjaxOut.HtmlContent = ex.Message.ToString();
        //    }
        //    return OAjaxOut;
        //}

        [AjaxPro.AjaxMethod]
        public string GetConfirmDeleteMsg(string SiteId)
        {
            return "Bạn đã chắc chắn xóa chưa?";
        }

        [AjaxPro.AjaxMethod]
        public string GetConfirmSetRightAnsItem(string SiteId, string AnswerId)
        {
            try
            {
                AnswerCls AnswerCls = new QuestionBll().CreateModelAnswer(SiteId, AnswerId);
                string retValue = "Bạn đã chắc chắn đặt câu trả lời [RightAnsText] là câu đúng chưa?";
                retValue = retValue.Replace("RightAnsText", AnswerCls.AnswerTextFirst);
                return retValue;
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public AjaxOut ServerSideDeleteAnswer(
            string SiteId,
            string AnswerId,
            string QuestionId,
            string QuestionType
            )
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                new QuestionBll().DeleteAnswerItem(null, SiteId, AnswerId);
                if (QuestionType.ToLower().Equals("crosslink"))
                {
                    OAjaxOut.HtmlContent = GetListAnswerCrossLink(SiteId, QuestionId, QuestionType).HtmlContent;
                }
                else if (QuestionType.ToLower().Equals("image"))
                {
                    OAjaxOut.HtmlContent = GetListAnswerImage(SiteId, QuestionId, QuestionType).HtmlContent;
                }
                else
                {
                    OAjaxOut.HtmlContent = GetListAnswer(SiteId, QuestionId, QuestionType).HtmlContent;
                }
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod]
        public string GetUpdateAnsItemText(
            string SiteId,
            string QuestionId,
            string AnswerId,
            string QuestionType)
        {
            try
            {
                WebSessionUtility.CheckSessionTimeOut();

                return "EditAnswerItem.aspx?QuestionId=" + QuestionId + "&AnswerId=" + AnswerId;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideSaveAnsItem(string SiteId, string AnswerId, string AnsText)
        {
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                if (AnsText.Trim().Length == 0)
                {
                    throw new Exception("Chưa nhập nội dung phương án trả lời mới!");
                }
                new QuestionBll().SaveAnswerItem(null, SiteId, AnswerId, AnsText);
                return "Cập nhật nội dung phương án trả lời thành công!";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public AjaxOut ServerSideSetRightAnsItem(string SiteId, string QuestionId, string AnswerId, string QuestionType)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                QuestionCls OQuestion = new QuestionBll().CreateModel(SiteId, QuestionId);
                //bool Reset = OQuestion.Type.ToLower().Equals("bestanswer");
                new QuestionBll().SetRightAnswerItem(null, SiteId, QuestionId, AnswerId, false);
                //return "Đặt lại phương án trả lời đúng thành công!";
                OAjaxOut.HtmlContent = GetListAnswer(SiteId, QuestionId, QuestionType).HtmlContent;

                string RightAns = "";
                string TrueAns = "Các phương án đúng: ";
                string FalseAns = "Các phương án sai: ";
                DataTable dtAnswers = new QuestionBll().ReadingAnswer(SiteId, QuestionId);
                for (int iIndexAns = 0; iIndexAns < dtAnswers.Rows.Count; iIndexAns++)
                {
                    string CorrectAnswer = "";
                    try
                    {
                        CorrectAnswer = (string)dtAnswers.Rows[iIndexAns]["CorrectAnswer"];
                    }
                    catch { }
                    CorrectAnswer = CorrectAnswer.ToUpper();
                    if (CorrectAnswer.Equals("YES"))
                    {
                        if (QuestionType.ToLower().Equals("Multichoice".ToLower()))
                        {
                            if (!string.IsNullOrEmpty(RightAns)) RightAns += "; ";
                            RightAns = RightAns + (string)dtAnswers.Rows[iIndexAns]["AnswerTextFirst"];
                        }
                        if (QuestionType.ToLower().Equals("BestAnswer".ToLower()))
                        {
                            if (!string.IsNullOrEmpty(RightAns)) RightAns += "; ";
                            RightAns = RightAns + (string)dtAnswers.Rows[iIndexAns]["AnswerTextFirst"];
                            string[] All = RightAns.Split(';');
                            if (All.Length > 1)
                            {
                                throw new Exception("Chọn câu trả lời đúng nhat!");
                            }
                        }
                        else if (QuestionType.ToLower().Equals("TrueFalse".ToLower()))
                        {
                            TrueAns += (string)dtAnswers.Rows[iIndexAns]["AnswerTextFirst"] + ";";
                        }

                    }
                    else
                    {
                        if (QuestionType.ToLower().Equals("TrueFalse".ToLower()))
                        {
                            FalseAns += (string)dtAnswers.Rows[iIndexAns]["AnswerTextFirst"] + ";";
                        }
                    }
                }
                if (QuestionType.ToLower().Equals("TrueFalse".ToLower()))
                {
                    RightAns = TrueAns + " - " + FalseAns;
                }
                OAjaxOut.RetExtraParam1 = RightAns;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.Error = true;
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod]
        public AjaxOut ServerSideRemoveRightAnsItem(
            string SiteId,
            string QuestionId,
            string AnswerId,
            string QuestionType)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                new QuestionBll().SetWrongAnswerItem(null, SiteId, QuestionId, AnswerId);
                OAjaxOut.HtmlContent = GetListAnswer(SiteId, QuestionId, QuestionType).HtmlContent;


                string RightAns = "";
                string TrueAns = "Các phương án đúng: ";
                string FalseAns = "Các phương án sai: ";
                DataTable dtAnswers = new QuestionBll().ReadingAnswer(SiteId, QuestionId);
                for (int iIndexAns = 0; iIndexAns < dtAnswers.Rows.Count; iIndexAns++)
                {
                    string CorrectAnswer = "";
                    try
                    {
                        CorrectAnswer = (string)dtAnswers.Rows[iIndexAns]["CorrectAnswer"];
                    }
                    catch { }
                    CorrectAnswer = CorrectAnswer.ToUpper();
                    if (CorrectAnswer.Equals("YES"))
                    {
                        try
                        {
                            //Multichoice
                            if (QuestionType.ToLower().Equals("Multichoice".ToLower()))
                            {
                                if (!string.IsNullOrEmpty(RightAns)) RightAns += "; ";
                                RightAns = RightAns + (string)dtAnswers.Rows[iIndexAns]["AnswerTextFirst"];
                            }
                            if (QuestionType.ToLower().Equals("BestAnswer".ToLower()))
                            {
                                if (!string.IsNullOrEmpty(RightAns)) RightAns += "; ";
                                RightAns = RightAns + (string)dtAnswers.Rows[iIndexAns]["AnswerTextFirst"];
                            }
                            else if (QuestionType.ToLower().Equals("TrueFalse".ToLower()))
                            {
                                TrueAns += (string)dtAnswers.Rows[iIndexAns]["AnswerTextFirst"] + ";";
                            }
                        }
                        catch { }
                    }
                    else
                    {
                        if (QuestionType.ToLower().Equals("TrueFalse".ToLower()))
                        {
                            FalseAns += (string)dtAnswers.Rows[iIndexAns]["AnswerTextFirst"] + ";";
                        }
                    }
                }
                if (QuestionType.ToLower().Equals("TrueFalse".ToLower()))
                {
                    RightAns = TrueAns + " - " + FalseAns;
                }
                OAjaxOut.RetExtraParam1 = RightAns;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.Error = true;
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod]
        public string ServerSideSaveQuestion(
            string SiteId,
            string QuestionId,
            string Title,
            string Question,
            int MaxPoint,
            string SelectLevel)
        {
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                if (string.IsNullOrEmpty(Title))
                {
                    throw new Exception("Chưa nhập nội dung câu hỏi!");
                }
                Title = Title.Trim();
                new QuestionBll().SaveQuestion(null, SiteId, QuestionId, Title, Question, MaxPoint, "", 0, false, 0, int.Parse(SelectLevel));
                return "Cập nhật câu hỏi thành công!";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }
    }
}